package com.mstore.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mstore.action.BaseAction;
import com.mstore.config.Constants;
import com.mstore.interceptor.Authority;
import com.mstore.pojo.Account;
import com.mstore.pojo.BFuncPrivilege;
import com.mstore.pojo.BFunction;
import com.mstore.pojo.Member;
import com.mstore.service.AccountService;
import com.mstore.service.BFuncPrivilegeService;
import com.mstore.service.BFunctionService;
import com.mstore.service.MemberService;
import com.mstore.service.UserService;
import com.mstore.service.alarm.AlarmServiceImpl;

public class UcenterRegisteAction extends BaseAction {

	private static final long serialVersionUID = 2654185972143819155L;

	private AccountService accountService;

	private MemberService memberService;

	private UserService userService;

	private AlarmServiceImpl alarmService;

	private BFunctionService functionService;

	private BFuncPrivilegeService funcPrivilegeService;

	private String result;

	private String mobile;

	private String password;

	private String email;

	private String type;

	/**
	 * 手机注册
	 */
	@Authority(actionName = "maccount", privilege = "pass")
	public String ucenterRegiste1() {
		// 保存到member表
		Member member = new Member();
		member.setMobile(mobile.trim());
		member.setPassword(password.trim());
		member.setType(Integer.parseInt(type));
		String createTime = super.getCreateTime();
		member.setCreateTime(super.getCreateTime());
		memberService.addMember(member);

		// 如果是企业注册
		// 保存到企业表
		if (member.getType() == 2) {
			Account account = new Account();
			account.setMobile(mobile.trim());
			account.setPassword(password.trim());
			account.setMember(member);
			account.setCreateTime(createTime);
			accountService.addAccount(account);

			initFunctionPrivilege(account.getId());
		}

		return "ucenterRegisteSuccess1";
	}

	@Authority(actionName = "maccount", privilege = "pass")
	public String sendMail() {

		alarmService.sendMimeMessage("无觅系统：请您验证邮箱", "点击链接即可注册<a href='"
				+ Constants.emailCallback
				+ "ucenterRegiste!ucenterRegiste2?email=" + email
				+ "&password=" + password + "&type=" + type + "'>"
				+ Constants.emailCallback
				+ "ucenterRegiste!ucenterRegiste2?email=" + email
				+ "&password=" + password + "&type=" + type + "</a>", email);

		return "sendMail";
	}

	/**
	 * 邮箱注册，注册邮件中的回调函数
	 */
	@Authority(actionName = "maccount", privilege = "pass")
	public String ucenterRegiste2() {
		Member member = new Member();
		member.setEmail(email.trim());
		boolean checkAccountEmail = accountService.checkActEmail(email);
		List<Object> memberList = new ArrayList<Object>();
		memberList = memberService.getMemberByEmail(member);
		if (checkAccountEmail || memberList.size() > 0) {

			return "ucenterRegisteError2";
		}
		else {
			// 保存到member表
			member.setPassword(password.trim());
			member.setType(Integer.parseInt(type));
			String createTime = super.getCreateTime();
			member.setCreateTime(createTime);
			memberService.addMember(member);

			// 如果是企业注册
			// 保存到企业表
			if (member.getType() == 2) {
				Account account = new Account();
				account.setEmail(email.trim());
				account.setPassword(password.trim());
				account.setMember(member);
				account.setCreateTime(createTime);
				accountService.addAccount(account);

				initFunctionPrivilege(account.getId());
			}
		}

		return "ucenterRegisteSuccess2";
	}

	/**
	 * 在member检查手机号
	 */
	@Authority(actionName = "maccount", privilege = "pass")
	public String checkMemberMobile() {
		Map<String, Object> map = new HashMap<String, Object>();
		Member member = new Member();
		member.setMobile(mobile);
		member = memberService.getmemberByMobile(member);
		if (member != null) {
			map.put("temp", "手机号已注册");
			result = mapTojson(map);
			return "checkMemberMobile";
		}
		map.put("temp", "手机可以注册");
		result = mapTojson(map);
		return "checkMemberMobile";
	}

	/**
	 * 验证邮箱
	 * 
	 * @return
	 */
	@Authority(actionName = "maccount", privilege = "pass")
	public String checkMemberEmail() {
		Map<String, Object> map = new HashMap<String, Object>();
		Member member = new Member();
		member.setEmail(email.trim());
		List<Object> objects = new ArrayList<Object>();
		objects = memberService.getMemberByEmail(member);
		if (objects.size() != 0) {
			map.put("temp", "邮箱已注册");
			result = mapTojson(map);
			return "checkMemberEmailSuccess";
		}
		map.put("temp", "邮箱可以注册");
		result = mapTojson(map);
		return "checkMemberEmailSuccess";
	}

	/**
	 * 初始化企业权限
	 */
	private void initFunctionPrivilege(Long eaId) {
		List<BFunction> functions = functionService.getAllFunction();
		for (BFunction function : functions) {
			BFuncPrivilege funcPrivilege = new BFuncPrivilege();
			funcPrivilege.setEaId(eaId);
			funcPrivilege.setFunctionId(function.getId());
			if (function.getLevel() == 0) {
				// 超级管理员权限
				funcPrivilege.setOnOff("0");
			}
			else {
				funcPrivilege.setOnOff("1");
			}
			funcPrivilegeService.addFuncPrivilege(funcPrivilege);
		}
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public AlarmServiceImpl getAlarmService() {
		return alarmService;
	}

	public void setAlarmService(AlarmServiceImpl alarmService) {
		this.alarmService = alarmService;
	}

	public BFunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(BFunctionService functionService) {
		this.functionService = functionService;
	}

	public BFuncPrivilegeService getFuncPrivilegeService() {
		return funcPrivilegeService;
	}

	public void setFuncPrivilegeService(
			BFuncPrivilegeService funcPrivilegeService) {
		this.funcPrivilegeService = funcPrivilegeService;
	}

}
